﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace SmartPhone.Un.Pro.DataContracts
{
    /// <summary> Object extension
    /// </summary>
    public static class ObjectExtension
    {
        #region constant


        #endregion

        #region object    

        /// <summary>
        /// object to string ;return empty if null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetStringValue(this object obj)
        {
            string value;
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                value = obj.ToString();
            }
            catch (Exception)
            {

                value = string.Empty;
            }
            return value;

        }

        #endregion

        #region Guid

        /// <summary>
        /// Convert  to guid
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Guid GetGuid(this Guid obj)
        {
            Guid guid = Guid.Empty;
            if (obj== null) return guid;
            return obj;
        }

        /// <summary>
        /// Check Guid is empty
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsGuidEmpty(this Guid obj)
        {
            if (obj.GetGuid() == Guid.Empty)
            { return true; }
            return false;
        }

        /// <summary>
        /// Get guid Empty if null ; other twice get guid value
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Guid GetGuidNullable(this Guid? obj)
        {
            Guid guid = Guid.Empty;
            if (obj == null)
            {
                return guid;
            }

            return obj.Value;
        }

        #endregion

        #region String

        /// <summary>
        /// Get String ; return empty if null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetString(this string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                return string.Empty;
            }
            else
            {
                return obj.Trim();
            }
        }

        /// <summary>
        /// ToListString with separate ',';'
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<string> StringToList(this string str)
        {
            var lis = new List<string>();
            foreach (string s in str.Split(new char[] { ',', ';' }))
            {
                lis.Add(s);
            }
            return lis;
        }

        public static string ListToString(this List<string> arrStr)
        {
            string returnValue = string.Empty;

            foreach (var item in arrStr)
            {
                returnValue += item + ",";
            }
            return returnValue;
        }

        #endregion

        #region Integer

        /// <summary>
        /// Get Integer or 0 if int is null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetInteger(this int? obj)
        {
            if (obj.HasValue)
            {
                return obj.Value;
            }
            else
            {
                return 0;
            }

        }

        #endregion

        #region Double

        /// <summary>
        /// Get double or 0 if double is null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double GetDouble(this double? obj)
        {
            if (obj == null) return 0.0;
            return obj.Value;
        }

        /// <summary>
        /// get double with pattern : ###,###.##
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetDoubleString(this double? obj)
        {
            if (obj != null)
            {
                return obj.Value.ToString(UnConstans.DoublePattern);
            }
            else
            {
                return "0";
            }
        }
        

        /// <summary>
        /// get double 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string GetDoubleString(this double? obj, string pattern)
        {
            if (obj != null)
            {
                return obj.Value.ToString(pattern);
            }
            else
            {
                return "0";
            }
        }
        #endregion

        #region bool
        /// <summary>
        /// Get  Bool value . if exception then get false
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool GetBool(this bool? obj)
        {
            if (obj == null) return false;
            return obj.Value;
        }
        #endregion
        
        #region date

        /// <summary>
        /// Convert string to date time. if exception then get 1900/01/01
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>Date time format vi-Vn</returns>
        public static DateTime GetDate(this string obj)
        {
            DateTime initDate = new DateTime(1900, 1, 1);
            DateTime dateReturn;
            string date = obj;
            IFormatProvider culture = new System.Globalization.CultureInfo(UnConstans.LanguageDefault, true);
            try
            {
                dateReturn = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
            }
            catch (Exception)
            {
                dateReturn = initDate;
            }
            return dateReturn;
        }

        /// <summary>
        /// /// Get date with pattern dd/MM/yyyy
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetDateString(this DateTime? obj)
        {
            if (obj == null) return "";
            return obj.Value.ToString(UnConstans.DatePattern);
            
        }

        /// <summary>
        /// Get date with pattern 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string GetDate(this DateTime? obj, string pattern)
        {
            if (obj == null) return "";
            return obj.Value.ToString(pattern);
        }

        /// <summary>
        /// Get HourMinute with pattern HH:mm
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetHourMinute(this DateTime? obj)
        {
            if (obj == null) return "";
            return obj.Value.ToString(UnConstans.HourMinutePattern);
        }

        /// <summary>
        /// Get date
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime GetDate(this DateTime? obj)
        {
            DateTime dtDate = new DateTime(1900, 1, 1);
            if (obj == null)
            {
                return dtDate;
            }

            return obj.Value;
        }

        #endregion

        #region cookies
        /// <summary>
        /// Save cookies 
        /// </summary>
        /// <param name="response"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SaveCookies(this System.Web.HttpResponseBase response, string key, string value)
        {
            DateTime expires = DateTime.Now.AddDays(30);
            if (response.Cookies[key] != null)
            {
                response.Cookies.Remove(key);
            }
            response.Cookies[key].Value = value;
            response.Cookies[key].Expires = expires;
        }

        /// <summary>
        /// Get cookies by key
        /// </summary>
        /// <param name="request"></param>
        /// <param name="key"></param>
        /// <returns>value of key</returns>
        public static string GetCookies(this System.Web.HttpRequestBase request, string key)
        {
            string value = string.Empty;
            if (request.Cookies[key] != null)
            {
                value = request.Cookies[key].Value.GetString();
            }
            return value;
        }


        #endregion

        #region session
        public static void SetSessionData<T>(this System.Web.HttpSessionStateBase session,string name, T value)
        {
            session[name] = value;
        }
        public static T GetSessionData<T>(this System.Web.HttpSessionStateBase session, string name)
        {
            try
            {
                object val = session[name];
                if (!(val == null))
                    return (T)val;
            }
            catch (Exception)
            {}
            return default(T);            
        }

        #endregion






















    }
}
